/*
 * Created on 2005-7-5
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.vsg.common.pagination.web.taglibs;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.Tag;

import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.web.taglibs.html.BaseStyleTag;
import org.vsg.common.pagination.wrapper.GenericWebPaginationWrapper;

/**
 * JSP Tag Handler class
 *
 * @jsp.tag   name = "TotalRecored"
 *            display-name = "Name for TotalRecored"
 *            description = "Description for TotalRecored"
 *            body-content = "empty"
 */
public class TotalRecoredTag extends BaseStyleTag {
	
	private String name;
	
	private String view = "false";
	
	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @param position The position to set.
	 */
	
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();

		Pagination pagination = null;
	
		// find the parent tag
		PaginationBarTag parent = (PaginationBarTag)findAncestorWithClass(this,PaginationBarTag.class);
		if (parent == null) {
			// get pagination bean
			Object objref = request.getAttribute(name);
			if (objref instanceof GenericWebPaginationWrapper) {
				GenericWebPaginationWrapper wpb = (GenericWebPaginationWrapper)objref;
				pagination = wpb.getPagination();
			}
		} else {
			pagination = parent.getPagination();
		}

		pageContext.setAttribute("totalRecord",new Integer(pagination.getTotalRecord()));

		
		Boolean isViewed = new Boolean(this.view);
		if (isViewed.booleanValue()) {
			JspWriter writer = pageContext.getOut();
			StringBuffer totalRecord = new StringBuffer();
			totalRecord.append(pagination.getTotalRecord());
			try {
				writer.write(totalRecord.toString());
			} catch (IOException ioe) {
				throw new JspException(ioe);
			}
		}
		return Tag.EVAL_BODY_INCLUDE;
	}
	
	public void setView(String view) {
		this.view = view;
	}

}
